import BaseView from "../../../cfw/mvc/BaseView";
import BigExportPage3Item from "./BigExportPage3Item";
import SDKManager from "../../SDKManager";
import { BannerDir } from "../../SDKConfig";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { DIR } from "../../../cfw/tools/Define";
import { ExportType } from "../BigExportC";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BigExportPage3 extends BaseView {



	@property({ type: cc.ScrollView, displayName: "scrollview$LScrollView" })
	scrollview$LScrollView: cc.ScrollView = null;

	@property({ type: cc.Layout, displayName: "content$LLayout" })
	content$LLayout: cc.Layout = null;

	@property({ type: cc.Button, displayName: "back$VButton" })
	back$VButton: cc.Button = null;

	@property(cc.Prefab)
	prefab: cc.Prefab = null;

	protected itemList: BigExportPage3Item[] = []
	protected buffer: BigExportPage3Item[] = []
	// protected isOk: boolean = true;
	// protected isJump: boolean = false;
	protected firstItem: BigExportPage3Item;
	protected dir: DIR = DIR.LEFT;
	protected clickState: number = 0;
	onLoad() {
		this.setTimeout(() => {
			this.back$VButton.node.active = true;
		}, 3000);
		GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)

		this.updateContent()
		// for (let index = 0; index < this.content$LLayout.length; index++) {
		// 	const element = this.content$LLayout[index];
		// 	element.node.x = cc.view.getVisibleSize().width;
		// cc.tween(this.content$LLayout.node).to(3, { x: -634 }).start()
		// }

		// if (this.firstItem) {
		// 	this.firstItem.onButtonClick()
		// }

	}


	update() {
		let width = cc.view.getVisibleSize().width;

		// for (let index = 0; index < this.content$LLayout.length; index++) {
		const element = this.content$LLayout
		let hw = element.node.width;
		if (hw <= width) {
			return;
		}
		if (this.dir == DIR.LEFT) {
			element.node.x -= 3;
			let x = hw - width + width / 2;
			if (element.node.x < - x) {
				element.node.x = -x;
				this.dir = DIR.RIGHT
			}
		} else {
			element.node.x += 3;
			let x = width / 2;
			if (element.node.x > - x) {
				element.node.x = -x
				this.dir = DIR.LEFT
			}
		}



	}
	onDestroy() {
		super.onDestroy()
		SDKManager.getChannel().hideBanner()
		GlobalEvent.instance().off(GlobalEvent.EVENT_SHOW, this.eventShow, this)
	}

	eventShow() {
		// console.log(' CrazyClickView eventShow',)
		// this.isJump = true;
		SDKManager.getChannel().hideBanner()
		SDKManager.getChannel().loadBanner()
		this.exit();
	}

	onback$VButtonClick() {
		let flag = SDKManager.getChannel().isBannerErrorClick()
		// flag = true;
		if (flag) {
			// if (!this.isOk) {
			// 	this.isOk = true;
			// 	return;
			// }
			switch (this.clickState) {
				case 0:
					this.clickState = 1;
					this.setTimeout(() => {


						if (SDKManager.getChannel().hasBanner()) {
							SDKManager.getChannel().showBanner(0, BannerDir.BOTTOM_MID)
							// this.isOk = false;
							this.setTimeout(() => {
								// if (cc.isValid(this.node)) {
								// this.isOk = true;

								SDKManager.getChannel().hideBanner()
								this.clickState = 2;
								// }
							}, 2000);
						} else {
							this.clickState = 2;
						}
					}, 1000);
					break;
				case 1:

					break;
				case 2:
					this.closeView();
					break;

			}

		} else {
			this.closeView();
		}


	}

	closeView() {
		this.hide();
		this.controller.closeView(ExportType.EXPORT3)
	}

	updateContent() {
		for (let index = 0; index < this.itemList.length; index++) {

			// for (let j = 0; j < this.itemList[index].length; j++) {
			const element = this.itemList[index];
			this.buffer.push(element)
			// }

		}
		this.itemList.length = 0;
		let list = this.model.getAdData()
		for (let index = 0; index < list.length; index++) {
			const element = list[index];
			if (element) {
				let ii = index
				// console.log(' index ================  i ')
				let comp = this.buffer.shift()
				if (!comp) {
					let node = cc.instantiate(this.prefab)
					comp = this.controller.initView(node, 'BigExportPage3Item', element) as BigExportPage3Item
					comp.setIndex(ii)
					// node.y = -200
					this.content$LLayout.node.addChild(node)
				} else {
					comp.setIndex(ii)
					comp.setModel(element)
					comp.refresh()
				}
				if (index == 0) {
					this.firstItem = comp
				}
				this.itemList.push(comp)
			}

		}
	}

}